package com.lanou.zakerproject.community;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.lanou.zakerproject.R;
import com.lanou.zakerproject.base.VolleySingleton;

/**
 * Created by dllo on 16/1/13.
 */
public class ChoiceAdapter extends BaseAdapter {
    private Context context;
    private CommunityData communityData;
    final int VIEW_TYPE = 4;
    final int TYPE_1 = 0;
    final int TYPE_2 = 1;
    final int TYPE_3 = 2;
    final int TYPE_4 = 3;

    public ChoiceAdapter(Context context, CommunityData communityData) {
        this.context = context;
        this.communityData = communityData;
    }

    @Override
    public int getCount() {
        return communityData.getData().getPosts().size();
    }

    //每个convert view都会调用此方法，获得当前所需要的view样式
    @Override
    public int getItemViewType(int position) {
        if (null != communityData.getData().getPosts().get(position).getSpecial_info().getItem_type()) {
            String TYPE = communityData.getData().getPosts().get(position).getSpecial_info().getItem_type();
            if (TYPE.equals("1")) {
                return TYPE_2;
            } else if (TYPE.equals("2")) {
                return TYPE_3;
            } else if (TYPE.equals("3")) {
                return TYPE_4;
            }
        }else {
            return 0;
        }
        return TYPE_1;
    }

    @Override
    public int getViewTypeCount() {
        return VIEW_TYPE;
    }

    @Override
    public Object getItem(int position) {
        return communityData.getData().getPosts().get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyViewHolder myViewHolder;
        OnePicViewHolder onePicViewHolder;
        TwoPicViewHolder twoPicViewHolder;
        ThreePicViewHolder threePicViewHolder;
        int type = getItemViewType(position);
        //无convertView，需要new出各个控件
        if (convertView == null) {
            switch (type) {
                case TYPE_1:
                    convertView = LayoutInflater.from(context).inflate(R.layout.item_community_choice_no_pic, parent, false);
                    myViewHolder = new MyViewHolder(convertView);
                    convertView.setTag(myViewHolder);

                    break;
                case TYPE_2:
                    convertView = LayoutInflater.from(context).inflate(R.layout.item_community_choice_one_pic, parent, false);
                    onePicViewHolder = new OnePicViewHolder(convertView);
                    convertView.setTag(onePicViewHolder);

                    break;
                case TYPE_3:
                    convertView = LayoutInflater.from(context).inflate(R.layout.item_community_choice_two_pic, parent, false);
                    twoPicViewHolder = new TwoPicViewHolder(convertView);
                    convertView.setTag(twoPicViewHolder);

                    break;
                case TYPE_4:
                    convertView = LayoutInflater.from(context).inflate(R.layout.item_community_choice_three_pic, parent, false);
                    threePicViewHolder = new ThreePicViewHolder(convertView);
                    convertView.setTag(threePicViewHolder);

                    break;
            }
        } else {
            switch (type) {
                case TYPE_1:
                    myViewHolder = (MyViewHolder) convertView.getTag();
                    ImageLoader.ImageListener listener1 = ImageLoader.getImageListener(myViewHolder.item_choice_personal_icon, R.mipmap.iconfont_picpng, R.mipmap.default_img_nonet);
                    VolleySingleton.getImageLoader().get(communityData.getData().getPosts().get(position).getAuther().getIcon(), listener1);
                    myViewHolder.item_choice_personal_name.setText(communityData.getData().getPosts().get(position).getAuther().getName());
                    myViewHolder.item_choice_personal_date.setText(communityData.getData().getPosts().get(position).getDate());
                    myViewHolder.item_choice_personal_content.setText(communityData.getData().getPosts().get(position).getContent());
                    myViewHolder.item_choice_personal_look.setText(communityData.getData().getPosts().get(position).getHot_num());
                    myViewHolder.item_choice_personal_comment.setText(communityData.getData().getPosts().get(position).getComment_count());
                    myViewHolder.item_choice_personal_zan.setText(communityData.getData().getPosts().get(position).getLike_num());

                    break;
                case TYPE_2:
                    onePicViewHolder = (OnePicViewHolder) convertView.getTag();
                    ImageLoader.ImageListener listener2_1 = ImageLoader.getImageListener(onePicViewHolder.item_choice_personal_icon_1, R.mipmap.iconfont_picpng, R.mipmap.default_img_nonet);
                    VolleySingleton.getImageLoader().get(communityData.getData().getPosts().get(position).getAuther().getIcon(), listener2_1);
                    ImageLoader.ImageListener listener2_2 = ImageLoader.getImageListener(onePicViewHolder.item_choice_personal_pic_1, R.mipmap.iconfont_picpng, R.mipmap.default_img_nonet);
                    VolleySingleton.getImageLoader().get(communityData.getData().getPosts().get(position).getThumbnail_medias().get(0).getUrl(), listener2_2);
                    onePicViewHolder.item_choice_personal_name_1.setText(communityData.getData().getPosts().get(position).getAuther().getName());
                    onePicViewHolder.item_choice_personal_date_1.setText(communityData.getData().getPosts().get(position).getDate());
                    onePicViewHolder.item_choice_personal_content_1.setText(communityData.getData().getPosts().get(position).getContent());
                    onePicViewHolder.item_choice_personal_look_1.setText(communityData.getData().getPosts().get(position).getHot_num());
                    onePicViewHolder.item_choice_personal_comment_count_1.setText(communityData.getData().getPosts().get(position).getComment_count());
                    onePicViewHolder.item_choice_personal_zan_1.setText(communityData.getData().getPosts().get(position).getLike_num());
                    break;
                case TYPE_3:
                    twoPicViewHolder = (TwoPicViewHolder) convertView.getTag();
                    ImageLoader.ImageListener listener3_1 = ImageLoader.getImageListener(twoPicViewHolder.item_choice_personal_icon_2, R.mipmap.iconfont_picpng, R.mipmap.default_img_nonet);
                    VolleySingleton.getImageLoader().get(communityData.getData().getPosts().get(position).getAuther().getIcon(), listener3_1);
                    ImageLoader.ImageListener listener3_2 = ImageLoader.getImageListener(twoPicViewHolder.item_choice_personal_pic_2, R.mipmap.iconfont_picpng, R.mipmap.default_img_nonet);
                    VolleySingleton.getImageLoader().get(communityData.getData().getPosts().get(position).getThumbnail_medias().get(0).getUrl(), listener3_2);
                    ImageLoader.ImageListener listener3_3 = ImageLoader.getImageListener(twoPicViewHolder.item_choice_personal_pic_2_2, R.mipmap.iconfont_picpng, R.mipmap.default_img_nonet);
                    VolleySingleton.getImageLoader().get(communityData.getData().getPosts().get(position).getThumbnail_medias().get(1).getUrl(), listener3_3);
                    twoPicViewHolder.item_choice_personal_name_2.setText(communityData.getData().getPosts().get(position).getAuther().getName());
                    twoPicViewHolder.item_choice_personal_date_2.setText(communityData.getData().getPosts().get(position).getDate());
                    twoPicViewHolder.item_choice_personal_content_2.setText(communityData.getData().getPosts().get(position).getContent());
                    break;
                case TYPE_4:
                    threePicViewHolder = (ThreePicViewHolder) convertView.getTag();
                    ImageLoader.ImageListener listener4_1 = ImageLoader.getImageListener(threePicViewHolder.item_choice_personal_icon_3, R.mipmap.iconfont_picpng, R.mipmap.default_img_nonet);
                    VolleySingleton.getImageLoader().get(communityData.getData().getPosts().get(position).getAuther().getIcon(), listener4_1);
                    ImageLoader.ImageListener listener4_2 = ImageLoader.getImageListener(threePicViewHolder.item_choice_personal_pic_3, R.mipmap.iconfont_picpng, R.mipmap.default_img_nonet);
                    VolleySingleton.getImageLoader().get(communityData.getData().getPosts().get(position).getThumbnail_medias().get(0).getUrl(), listener4_2);
                    ImageLoader.ImageListener listener4_3 = ImageLoader.getImageListener(threePicViewHolder.item_choice_personal_pic_3_2, R.mipmap.iconfont_picpng, R.mipmap.default_img_nonet);
                    VolleySingleton.getImageLoader().get(communityData.getData().getPosts().get(position).getThumbnail_medias().get(1).getUrl(), listener4_3);
                    ImageLoader.ImageListener listener4_4 = ImageLoader.getImageListener(threePicViewHolder.item_choice_personal_pic_3_3, R.mipmap.iconfont_picpng, R.mipmap.default_img_nonet);
                    VolleySingleton.getImageLoader().get(communityData.getData().getPosts().get(position).getThumbnail_medias().get(2).getUrl(), listener4_4);
                    threePicViewHolder.item_choice_personal_name_3.setText(communityData.getData().getPosts().get(position).getAuther().getName());
                    threePicViewHolder.item_choice_personal_date_3.setText(communityData.getData().getPosts().get(position).getDate());
                    threePicViewHolder.item_choice_personal_content_3.setText(communityData.getData().getPosts().get(position).getContent());
                    break;
            }
        }
        return convertView;
    }

    class MyViewHolder {
        private ImageView item_choice_personal_icon;
        private TextView item_choice_personal_name, item_choice_personal_date, item_choice_personal_content,item_choice_personal_look,item_choice_personal_comment,item_choice_personal_zan;

        public MyViewHolder(View view) {
            item_choice_personal_icon = (ImageView) view.findViewById(R.id.personal_icon);
            item_choice_personal_name = (TextView) view.findViewById(R.id.personal_name);
            item_choice_personal_date = (TextView) view.findViewById(R.id.personal_date);
            item_choice_personal_content = (TextView) view.findViewById(R.id.personal_content);
            item_choice_personal_look = (TextView) view.findViewById(R.id.personal_look);
            item_choice_personal_comment = (TextView) view.findViewById(R.id.personal_comment_);
            item_choice_personal_zan = (TextView) view.findViewById(R.id.personal_comment_zan);
        }
    }

    class OnePicViewHolder {
        private ImageView item_choice_personal_icon_1, item_choice_personal_pic_1;
        private TextView item_choice_personal_name_1, item_choice_personal_date_1, item_choice_personal_content_1,item_choice_personal_look_1,item_choice_personal_comment_count_1,item_choice_personal_zan_1;

        public OnePicViewHolder(View view) {
            item_choice_personal_icon_1 = (ImageView) view.findViewById(R.id.personal_icon_1);
            item_choice_personal_name_1 = (TextView) view.findViewById(R.id.personal_name_1);
            item_choice_personal_date_1 = (TextView) view.findViewById(R.id.personal_date_1);
            item_choice_personal_content_1 = (TextView) view.findViewById(R.id.personal_content_1);
            item_choice_personal_pic_1 = (ImageView) view.findViewById(R.id.personal_pic_1);
            item_choice_personal_look_1 = (TextView) view.findViewById(R.id.personal_look_1);
            item_choice_personal_comment_count_1 = (TextView) view.findViewById(R.id.personal_comment_count_1);
            item_choice_personal_zan_1 = (TextView) view.findViewById(R.id.personal_comment_zan_1);
        }
    }

    class TwoPicViewHolder {
        private ImageView item_choice_personal_icon_2, item_choice_personal_pic_2, item_choice_personal_pic_2_2;
        private TextView item_choice_personal_name_2, item_choice_personal_date_2, item_choice_personal_content_2;

        public TwoPicViewHolder(View view) {
            item_choice_personal_icon_2 = (ImageView) view.findViewById(R.id.personal_icon_2);
            item_choice_personal_name_2 = (TextView) view.findViewById(R.id.personal_name_2);
            item_choice_personal_date_2 = (TextView) view.findViewById(R.id.personal_date_2);
            item_choice_personal_content_2 = (TextView) view.findViewById(R.id.personal_content_2);
            item_choice_personal_pic_2 = (ImageView) view.findViewById(R.id.personal_pic_2);
            item_choice_personal_pic_2_2 = (ImageView) view.findViewById(R.id.personal_pic_2_2);
        }
    }

    class ThreePicViewHolder {
        private ImageView item_choice_personal_icon_3, item_choice_personal_pic_3, item_choice_personal_pic_3_2, item_choice_personal_pic_3_3;
        private TextView item_choice_personal_name_3, item_choice_personal_date_3, item_choice_personal_content_3;

        public ThreePicViewHolder(View view) {
            item_choice_personal_icon_3 = (ImageView) view.findViewById(R.id.personal_icon_3);
            item_choice_personal_name_3 = (TextView) view.findViewById(R.id.personal_name_3);
            item_choice_personal_date_3 = (TextView) view.findViewById(R.id.personal_date_3);
            item_choice_personal_content_3 = (TextView) view.findViewById(R.id.personal_content_3);
            item_choice_personal_pic_3 = (ImageView) view.findViewById(R.id.personal_pic_3);
            item_choice_personal_pic_3_2 = (ImageView) view.findViewById(R.id.personal_pic_3_2);
            item_choice_personal_pic_3_3 = (ImageView) view.findViewById(R.id.personal_pic_3_3);
        }
    }

}
